<!DOCTYPE html>


<html lang="zh-CN">


<head>
  <meta charset="utf-8" />
    
  <meta name="description" content="welcome" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Feign服务调用和负载均衡 |  ChenyyのBlog
  </title>
  <meta name="generator" content="hexo-theme-ayer">
  
  <link rel="shortcut icon" href="/favicon.ico" />
  
  
<link rel="stylesheet" href="/dist/main.css">

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Shen-Yu/cdn/css/remixicon.min.css">
  
<link rel="stylesheet" href="/css/custom.css">

  
  <script src="https://cdn.jsdelivr.net/npm/pace-js@1.0.2/pace.min.js"></script>
  
  

  

</head>

</html>

<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-framework/Feign服务调用和负载均衡"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Feign服务调用和负载均衡
</h1>
 

    </header>
     
    <div class="article-meta">
      <a href="/2021/06/26/framework/Feign%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8%E5%92%8C%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" class="article-date">
  <time datetime="2021-06-26T13:21:21.000Z" itemprop="datePublished">2021-06-26</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Spring-Cloud/">Spring Cloud</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">2.3k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">9 分钟</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <ul>
<li>介绍</li>
<li>Feign执行过程</li>
<li>Feign注解作用及原理</li>
<li>Ribbon负载均衡</li>
</ul>
<span id="more"></span>



<h1 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h1><p>Feign是Netflix公司开源的一个http请求调用的轻量级框架，可以以Java接口注解的方式调用Http请求。Spring Cloud引入Feign并且集成了Ribbon实现客户端负载均衡调用。</p>
<p>Feign 的英文表意为“假装，伪装，变形”。Feign通过处理注解，将请求模板化，当实际调用的时候，传入参数，根据参数再应用到请求上，进而转化成真正的请求。</p>
<h2 id="作用"><a href="#作用" class="headerlink" title="作用"></a>作用</h2><p><strong>封装Http调用流程，更适合面向接口化的变成习惯</strong></p>
<p>传统基于Http协议的服务框架有HttpURLConnection、Apache HttpComponnets、OkHttp3 、Netty等等，这些框架在基于自身的专注点提供了自身特性。而从角色划分上来看，他们的职能是一致的提供Http调用服务。</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626220051.png"></p>
<h2 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h2><ul>
<li>启动类加上@EnableFeignClients</li>
<li>在接口上使用@FeignClient(“服务名”)</li>
</ul>
<h1 id="Feign执行过程"><a href="#Feign执行过程" class="headerlink" title="Feign执行过程"></a>Feign执行过程</h1><p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626223036.png"></p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626220151.png"></p>
<h2 id="基于面向接口的动态代理方式生成实现类"><a href="#基于面向接口的动态代理方式生成实现类" class="headerlink" title="基于面向接口的动态代理方式生成实现类"></a>基于面向接口的动态代理方式生成实现类</h2><p>在使用feign时，会定义对应的接口类，在接口类上使用Http相关的注解，标识HTTP请求参数信息。</p>
<p>在Feign 底层，通过基于面向接口的动态代理方式生成实现类，将请求调用委托到动态代理实现类。</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626220243.png"></p>
<h2 id="根据Contract协议规则，解析接口类的注解信息"><a href="#根据Contract协议规则，解析接口类的注解信息" class="headerlink" title="根据Contract协议规则，解析接口类的注解信息"></a>根据Contract协议规则，解析接口类的注解信息</h2><p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626220319.png"></p>
<p>例如尝试调用GitHub.getContributors(“foo”,”myrepo”)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">public interface GitHub &#123;</span><br><span class="line">  </span><br><span class="line">  @RequestLine(&quot;GET /repos/&#123;owner&#125;/&#123;repo&#125;&quot;)</span><br><span class="line">  List&lt;Contributor&gt; getContributors(@Param(&quot;owner&quot;) String owner, @Param(&quot;repo&quot;) String repository);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>最终会转换成如下的HTTP请求：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET /repos/foo/myrepo</span><br><span class="line">HOST XXXX.XXX.XXX</span><br></pre></td></tr></table></figure>

<p><strong>Feign 默认的协议规范</strong></p>
<table>
<thead>
<tr>
<th>注解</th>
<th>接口Target</th>
<th>使用说明</th>
</tr>
</thead>
<tbody><tr>
<td><code>@RequestLine</code></td>
<td>方法上</td>
<td>定义HttpMethod 和 UriTemplate. UriTemplate 中使用<code>&#123;&#125;</code> 包裹的表达式，可以通过在方法参数上使用@Param 自动注入</td>
</tr>
<tr>
<td><code>@Param</code></td>
<td>方法参数</td>
<td>定义模板变量，模板变量的值可以使用名称的方式使用模板注入解析</td>
</tr>
<tr>
<td><code>@Headers</code></td>
<td>类上或者方法上</td>
<td>定义头部模板变量，使用@Param 注解提供参数值的注入。如果该注解添加在接口类上，则所有的请求都会携带对应的Header信息；如果在方法上，则只会添加到对应的方法请求上</td>
</tr>
<tr>
<td><code>@QueryMap</code></td>
<td>方法上</td>
<td>定义一个键值对或者 pojo，参数值将会被转换成URL上的 query 字符串上</td>
</tr>
<tr>
<td><code>@HeaderMap</code></td>
<td>方法上</td>
<td>定义一个HeaderMap, 与 UrlTemplate 和HeaderTemplate 类型，可以使用@Param 注解提供参数值</td>
</tr>
</tbody></table>
<p>FeignContract 解析参考代码：<a target="_blank" rel="noopener" href="https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/Contract.java">https://github.com/OpenFeign/feign/blob/master/core/src/main/java/feign/Contract.java</a></p>
<p><strong>基于Spring MVC的协议规范SpringMvcContract</strong></p>
<p>写客户端请求接口和像写服务端代码一样：客户端和服务端可以通过SDK的方式进行约定，客户端只需要引入服务端发布的SDK API，就可以使用面向接口的编码方式对接服务.</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626221613.png"></p>
<p>接口抽象出来的好处就是可以根据服务调用实现方式自有切换：</p>
<ol>
<li>可以基于简单的Http服务调用；</li>
<li>可以基于Spring Cloud 微服务架构调用；</li>
<li>可以基于Dubbo SOA服务治理</li>
</ol>
<p>Spring Cloud 是如何实现 协议解析的，可参考代码：<a target="_blank" rel="noopener" href="https://github.com/spring-cloud/spring-cloud-openfeign/blob/master/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringMvcContract.java">https://github.com/spring-cloud/spring-cloud-openfeign/blob/master/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/support/SpringMvcContract.java</a></p>
<h2 id="基于-RequestBean，动态生成Request"><a href="#基于-RequestBean，动态生成Request" class="headerlink" title="基于 RequestBean，动态生成Request"></a>基于 RequestBean，动态生成Request</h2><p>根据传入的Bean对象和注解信息，从中提取出相应的值，来构造Http Request 对象。</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626221939.png"></p>
<h2 id="使用Encoder-将Bean转换成-Http报文正文（消息解析和转码逻辑）"><a href="#使用Encoder-将Bean转换成-Http报文正文（消息解析和转码逻辑）" class="headerlink" title="使用Encoder 将Bean转换成 Http报文正文（消息解析和转码逻辑）"></a>使用Encoder 将Bean转换成 Http报文正文（消息解析和转码逻辑）</h2><p>Feign 最终会将请求转换成Http 消息发送出去，传入的请求对象最终会解析成消息体。</p>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626222046.png"></p>
<h2 id="拦截器负责对请求和返回进行装饰处理"><a href="#拦截器负责对请求和返回进行装饰处理" class="headerlink" title="拦截器负责对请求和返回进行装饰处理"></a>拦截器负责对请求和返回进行装饰处理</h2><p>在请求转换的过程中，Feign 抽象出来了拦截器接口，用于用户自定义对请求的操作。</p>
<h2 id="日志记录"><a href="#日志记录" class="headerlink" title="日志记录"></a>日志记录</h2><p>在发送和接收请求的时候，Feign定义了统一的日志门面来输出日志信息 , 并且将日志的输出定义了四个等级：</p>
<table>
<thead>
<tr>
<th align="left">级别</th>
<th align="left">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="left">NONE</td>
<td align="left">不做任何记录</td>
</tr>
<tr>
<td align="left">BASIC</td>
<td align="left">只记录输出Http 方法名称、请求URL、返回状态码和执行时间</td>
</tr>
<tr>
<td align="left">HEADERS</td>
<td align="left">记录输出Http 方法名称、请求URL、返回状态码和执行时间 和 Header 信息</td>
</tr>
<tr>
<td align="left">FULL</td>
<td align="left">记录Request 和Response的Header，Body和一些请求元数据</td>
</tr>
</tbody></table>
<h2 id="基于重试器发送HTTP请求"><a href="#基于重试器发送HTTP请求" class="headerlink" title="基于重试器发送HTTP请求"></a>基于重试器发送HTTP请求</h2><p>Feign 内置了一个重试器，当HTTP请求出现IO异常时，Feign会有一个最大尝试次数发送请求。</p>
<h2 id="发送Http请求"><a href="#发送Http请求" class="headerlink" title="发送Http请求"></a>发送Http请求</h2><p>Feign 真正发送HTTP请求是委托给 <code>feign.Client</code> 来做。</p>
<p> Feign 默认底层通过JDK 的 <code>java.net.HttpURLConnection</code> 实现了<code>feign.Client</code>接口类，在每次发送请求的时候，都会创建新的HttpURLConnection 链接，导致默认情况下Feign的性能很差。</p>
<p>可以通过拓展Client接口，使用Apache HttpClient 或者OkHttp3等基于连接池的高性能Http客户端。只需要引入对应依赖，并在配置文件中配置feign.httpclient.enabled为true（默认true）。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 使用HttpClient作为网络请求框架 --&gt;</span><br><span class="line">&lt;dependency&gt; </span><br><span class="line">    &lt;groupId&gt;io.github.openfeign&lt;/groupId&gt; </span><br><span class="line">    &lt;artifactId&gt;feign-httpclient&lt;/artifactId&gt; </span><br><span class="line">    &lt;version&gt;9.4.0&lt;/version&gt; </span><br><span class="line">&lt;/dependency&gt;</span><br><span class="line">&lt;!-- 使用OkHttp作为网络请求框架 --&gt;</span><br><span class="line">&lt;dependency&gt;</span><br><span class="line">    &lt;groupId&gt;io.github.openfeign&lt;/groupId&gt;</span><br><span class="line">    &lt;artifactId&gt;feign-okhttp&lt;/artifactId&gt;</span><br><span class="line">    &lt;version&gt;10.2.0&lt;/version&gt;</span><br><span class="line">&lt;/dependency&gt;</span><br></pre></td></tr></table></figure>



<h1 id="Feign注解作用及原理"><a href="#Feign注解作用及原理" class="headerlink" title="Feign注解作用及原理"></a>Feign注解作用及原理</h1><h2 id="EnableFeignClients作用"><a href="#EnableFeignClients作用" class="headerlink" title="@EnableFeignClients作用"></a>@EnableFeignClients作用</h2><ul>
<li>用于表示该客户端开启Feign方式调用、</li>
<li>创建FeignClient的工厂Bean，FeignClientFactoryBean通过@FeignClient收集调用信息（服务、接口等），创建动态代理对象。</li>
</ul>
<h2 id="EnableFeignClients：动态注册FeignClientFactoryBean"><a href="#EnableFeignClients：动态注册FeignClientFactoryBean" class="headerlink" title="@EnableFeignClients：动态注册FeignClientFactoryBean"></a>@EnableFeignClients：动态注册FeignClientFactoryBean</h2><p>在A中需要访问B和C的微服务接口，这个时候A中需要引用bClient、cClient、而FeignClientFactoryBean负责生成bClient、cClient的动态代理类。</p>
<p>@EnableFeignClients作为复合注解，使用<code>@Import(FeignClientsRegistrar.class)</code>导入Registar。</p>
<p>容器初始化时，是由FeignClientsRegistrar完成动态注册bean。</p>
<h2 id="FeignClient：标识业务调用的Client，采用动态注册的方式生成动态代理。"><a href="#FeignClient：标识业务调用的Client，采用动态注册的方式生成动态代理。" class="headerlink" title="@FeignClient：标识业务调用的Client，采用动态注册的方式生成动态代理。"></a>@FeignClient：标识业务调用的Client，采用动态注册的方式生成动态代理。</h2><p>通过配置类，或者package路径做扫描，收集FeignClient的静态信息，每个Client会把他的基本信息，类名、方法、服务名等绑定到FactoryBean上,这样就就具备了生成一个动态代理类的基本条件</p>
<h2 id="FeignClient基于Ribbon实现负载均衡，通过http去调用。"><a href="#FeignClient基于Ribbon实现负载均衡，通过http去调用。" class="headerlink" title="FeignClient基于Ribbon实现负载均衡，通过http去调用。"></a>FeignClient基于Ribbon实现负载均衡，通过http去调用。</h2><p>如果用户在FeignClient的注解中直接使用了URL，这种方式一般用于调试环境，直接指定一个服务的绝对地址，这种情况下不会走负载均衡，走默认的Client。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> Response <span class="title">execute</span><span class="params">(Request request, Options options)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">  HttpURLConnection connection = convertAndSend(request, options);</span><br><span class="line">  <span class="keyword">return</span> convertResponse(connection).toBuilder().request(request).build();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>如果用户在FeignClient中使用了seriveName，那么请求地址将会是<a target="_blank" rel="noopener" href="http://servicename,这种情况下是需要走负载均衡的.feign的负载均衡也是基于ribbon实现./">http://serviceName，这种情况下是需要走负载均衡的。Feign的负载均衡也是基于Ribbon实现。</a></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> Response <span class="title">execute</span><span class="params">(Request request, Request.Options options)</span> <span class="keyword">throws</span> IOException </span>&#123;</span><br><span class="line">	<span class="keyword">try</span> &#123;</span><br><span class="line">		URI asUri = URI.create(request.url());</span><br><span class="line">		String clientName = asUri.getHost();</span><br><span class="line">		URI uriWithoutHost = cleanUrl(request.url(), clientName);</span><br><span class="line">		FeignLoadBalancer.RibbonRequest ribbonRequest = <span class="keyword">new</span> FeignLoadBalancer.RibbonRequest(</span><br><span class="line">				<span class="keyword">this</span>.delegate, request, uriWithoutHost);</span><br><span class="line"></span><br><span class="line">		IClientConfig requestConfig = getClientConfig(options, clientName);</span><br><span class="line">		<span class="keyword">return</span> lbClient(clientName).executeWithLoadBalancer(ribbonRequest,</span><br><span class="line">				requestConfig).toResponse();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">catch</span> (ClientException e) &#123;</span><br><span class="line">		IOException io = findIOException(e);</span><br><span class="line">		<span class="keyword">if</span> (io != <span class="keyword">null</span>) &#123;</span><br><span class="line">			<span class="keyword">throw</span> io;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(e);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h1 id="Ribbon"><a href="#Ribbon" class="headerlink" title="Ribbon"></a>Ribbon</h1><p>Ribbon是Netflix公司开源的一个负载均衡的项目，它是一个基于 HTTP、TCP的客户端负载均衡器。</p>
<h2 id="作用-1"><a href="#作用-1" class="headerlink" title="作用"></a>作用</h2><p>将请求平均分配到多个服务，实现服务高可用。</p>
<h2 id="使用-1"><a href="#使用-1" class="headerlink" title="使用"></a>使用</h2><ul>
<li><p>使用@RibbonClient(value=”服务名称”) </p>
</li>
<li><p>使用RestTemplate调用远程服务对应的方法</p>
</li>
</ul>
<h2 id="对比"><a href="#对比" class="headerlink" title="对比"></a>对比</h2><ul>
<li><p>Ribbon需要自己构建模拟http请求，使用RestTemplate发送给其他服务。</p>
</li>
<li><p>Feign采用接口的方式，将调用服务的方法定义成抽象方法即可。</p>
</li>
</ul>
<h2 id="负载均衡"><a href="#负载均衡" class="headerlink" title="负载均衡"></a>负载均衡</h2><p>Ribbon默认为我们提供了很多的负载均衡算法，例如轮询、随机等。我们也可以自定负载均衡算法。</p>
<p>在消费微服务中使用Ribbon实现负载均衡，Ribbon先从EurekaServer中获取服务列表；Ribbon根据负载均衡的算法去调用微服务。</p>
<p>负载均衡策略：</p>
<ul>
<li>RoundRobinRule：轮询</li>
<li>RandomRule：随机</li>
<li>AvailabilityFilteringRule：会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务，还有并发的连接数量超过阈值的服务，然后对剩余的服务列表按照轮询策略进行访问。</li>
<li>WeightedResponseTimeRule：根据平均响应时间计算所有服务的权重，响应时间越快服务权重越大被选中的概率越高。刚启动时如果统计信息不足，则使用RoundRobinRule策略，等统计信息足够，会切换到WeightedResponseTimeRule。</li>
<li>RetryRule：先按照RoundRobinRule的策略获取服务，如果获取服务失败则在指定时间内会进行重试，获取可用的服务。</li>
<li>BestAvailableRule：会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务，然后选择一个并发量最小的服务。</li>
<li>ZoneAvoidanceRule：默认规则，复合判断server所在区域的性能和server的可用性选择服务器。</li>
</ul>
<p><img src="https://gitee.com/chenyy-2017/pic/raw/master/note/20210626214639.png"></p>
<p>参考：</p>
<ul>
<li>Feign开源项目地址：<a target="_blank" rel="noopener" href="https://github.com/OpenFeign/feign">https://github.com/OpenFeign/feign</a></li>
<li>Ribbon开源项目地址：<a target="_blank" rel="noopener" href="https://github.com/Netflix/ribbon">https://github.com/Netflix/ribbon</a></li>
<li>Feign负载均衡原理解析：<a target="_blank" rel="noopener" href="https://my.oschina.net/13426421702/blog/3064709">https://my.oschina.net/13426421702/blog/3064709</a></li>
<li>客户端负载均衡Feign之三：Feign设计原理：<a target="_blank" rel="noopener" href="https://www.cnblogs.com/duanxz/p/7519819.html">https://www.cnblogs.com/duanxz/p/7519819.html</a></li>
</ul>
 
      <!-- reward -->
      
      <div id="reword-out">
        <div id="reward-btn">
          打赏
        </div>
      </div>
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>版权声明： </strong>
          
          本博客所有文章除特别声明外，著作权归作者所有。转载请注明出处！
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2021/06/26/framework/Feign%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8%E5%92%8C%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Spring-Cloud/" rel="tag">Spring Cloud</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2021/06/26/component/Elasticsearch/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Elasticsearch
          
        </div>
      </a>
    
    
      <a href="/2021/06/26/framework/Nacos%E5%88%86%E7%BB%84%E9%9A%94%E7%A6%BB/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Nacos分组隔离</div>
      </a>
    
  </nav>

   
<!-- valine评论 -->
<div id="vcomments-box">
  <div id="vcomments"></div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine@1.4.14/dist/Valine.min.js"></script>
<script>
  new Valine({
    el: "#vcomments",
    app_id: "",
    app_key: "",
    path: window.location.pathname,
    avatar: "monsterid",
    placeholder: "给我的文章加点评论吧~",
    recordIP: true,
  });
  const infoEle = document.querySelector("#vcomments .info");
  if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
    infoEle.childNodes.forEach(function (item) {
      item.parentNode.removeChild(item);
    });
  }
</script>
<style>
  #vcomments-box {
    padding: 5px 30px;
  }

  @media screen and (max-width: 800px) {
    #vcomments-box {
      padding: 5px 0px;
    }
  }

  #vcomments-box #vcomments {
    background-color: #fff;
  }

  .v .vlist .vcard .vh {
    padding-right: 20px;
  }

  .v .vlist .vcard {
    padding-left: 10px;
  }
</style>

 
   
     
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2021
        <i class="ri-heart-fill heart_icon"></i> Chenyy
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>访问人数:<span id="busuanzi_value_site_uv"></span></s>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>浏览次数:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
      </li>
    </ul>
  </div>
</footer>
      <div class="float_btns">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

<div class="todark" id="todark">
  <i class="ri-moon-line"></i>
</div>

      </div>
    </main>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="ChenyyのBlog"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">文章</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags/book">收藏</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about/me">关于我</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="搜索">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <script>
      if (window.matchMedia("(max-width: 768px)").matches) {
        document.querySelector('.content').classList.remove('on');
        document.querySelector('.sidebar').classList.remove('on');
      }
    </script>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    
  </div>
</div>
    
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->


<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: 'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto'
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">
<script src="https://cdn.jsdelivr.net/npm/justifiedGallery@3.7.0/dist/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->


<link rel="stylesheet" href="/css/clipboard.css">

<script src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>


<!-- CanvasBackground -->


    
  </div>
</body>

</html>